package com.songcha.library_common.ui.view;

import android.content.Context;
import android.util.AttributeSet;
import android.widget.HorizontalScrollView;


/***
 * horizontal bounce view
 */

public class BounceHorizontalScrollView extends HorizontalScrollView{
    private static final int MAX_SCROLL = 200;
    private static final float SCROLL_RATIO = 0.5f;// 阻尼系数

    public BounceHorizontalScrollView(Context context)
    {
        super(context);
    }

    public BounceHorizontalScrollView(Context context, AttributeSet attrs)
    {
        super(context, attrs);
    }

    public BounceHorizontalScrollView(Context context, AttributeSet attrs, int defStyle)
    {
        super(context, attrs, defStyle);
    }

    @Override
    protected boolean overScrollBy(int deltaX, int deltaY, int scrollX, int scrollY, int scrollRangeX, int scrollRangeY, int maxOverScrollX, int maxOverScrollY, boolean isTouchEvent)
    {
        int newDeltaX = deltaX;
        int delta = (int) (deltaX * SCROLL_RATIO);
        if((scrollX+deltaX)==0 || (scrollX-scrollRangeX+deltaX)==0){
            newDeltaX = deltaX;  // bounce last scroll,reset
        }else{
            newDeltaX = delta;  //add damping effect
        }
        return super.overScrollBy(newDeltaX, deltaY, scrollX, scrollY, scrollRangeX, scrollRangeY, MAX_SCROLL, maxOverScrollY, isTouchEvent);
    }
}
